package com.inspur.myapplication.view;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.inspur.myapplication.R;
import com.inspur.myapplication.dao.GoodsDao;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.ArrayList;
import java.util.HashMap;

public class GoodsListActivity extends AppCompatActivity {
    private ArrayList goodsList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_goods_list);

        //接收其他组件传递的数据
        Intent intent=getIntent();
        String category=intent.getStringExtra("category");
        Log.i(GoodsListActivity.class.toString(),category);
        GoodsDao goodsDao=new GoodsDao(this);
        goodsList=goodsDao.queryGoods(null,category);
        /*
        //准备数据
        goodsList=new ArrayList();
        HashMap hashMap=new HashMap();
        hashMap.put("goodsName","goodsName1");
        hashMap.put("goodsPrice","20");
        hashMap.put("goodsImg",R.drawable.img);
        goodsList.add(hashMap);
        hashMap=new HashMap();
        hashMap.put("goodsName","goodsName2");
        hashMap.put("goodsPrice","21");
        hashMap.put("goodsImg",R.drawable.img);
        goodsList.add(hashMap);
        hashMap=new HashMap();
        hashMap.put("goodsName","goodsName3");
        hashMap.put("goodsPrice","22");
        hashMap.put("goodsImg",R.drawable.img);
        goodsList.add(hashMap);
        */

        //实例化数据适配器
        SimpleAdapter simpleAdapter=new SimpleAdapter(this,goodsList,R.layout.layout_goods_item,new String[]{"goodsName","goodsPrice","goodsImg"},new int[]{R.id.goods_item_name,R.id.goods_item_price,R.id.goods_item_iv});
        //实例化ImageLoader
        final ImageLoader imageLoader=ImageLoader.getInstance();

        //进行初始化
        ImageLoaderConfiguration imageLoaderConfiguration=new  ImageLoaderConfiguration.Builder(this).build();
        imageLoader.init(imageLoaderConfiguration);
        //通过ViewBinder来干预数据的显示
        simpleAdapter.setViewBinder(new SimpleAdapter.ViewBinder() {
            @Override
            public boolean setViewValue(View view, Object o, String s) {
                //view显示数据的组件，o实际显示的数据
                if(view.getId()==R.id.goods_item_iv){
                    imageLoader.displayImage(o.toString(),(ImageView) view);
                    return true;//如果返回true，代表ViewBinder来处理数据的显示
                }
                return false;//false，代表simpleAdapter自行来处理数据的显示
            }
        });

        ListView listView=(ListView) findViewById(R.id.goods_list_lv);
        listView.setAdapter(simpleAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int index, long l) {
                String goodsName=((HashMap)goodsList.get(index)).get("goodsName").toString();
                Toast.makeText(GoodsListActivity.this,goodsName,Toast.LENGTH_LONG).show();
            }
        });
    }
}
